package org.cloudnote.bean;

import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

@Component //扫描到spring
@Aspect //切面编程
public class ExceptionBean {
    @AfterThrowing(throwing = "e",pointcut = "within(org.cloudnote.controller..*)")
    public void printException(Exception e) {
        System.out.println("发生异常"+e);
        try {
            FileWriter fileWriter = new FileWriter("cloud_note.log", true);
            PrintWriter printWriter = new PrintWriter(fileWriter);
            e.printStackTrace(printWriter);
            printWriter.flush();
            printWriter.close();
            fileWriter.close();
        } catch (IOException ex) {
            System.out.println("记录异常信息失败"+ex);
        }
    }
}
